package com.servlet;

import com.bean.Book;
import com.bean.BookPage;
import com.bean.User;
import com.dao.BookDao;
import com.utils.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName BookServlet
 * @Desc 前台图书控制层
 * @Author xiaoding
 * @Date 2021/1/16 20:22
 * @Version 1.0
 */
@WebServlet(name = "BookServlet",value = "/BookServlet")
public class BookServlet extends HttpServlet {
    private BookDao bookDao = new BookDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能模块
        String str = request.getParameter("flag");

        //判断功能模块
        if (str.equals("select")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //查询所有图书的数量
            int count = bookDao.bookCount();
            //定义访问的Servlet
            String targetUrl = "BookServlet";
            //定义参数
            String param = "flag=select";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<Book> book = bookDao.bookSelect(new BookPage(page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("book",book);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("pre/index.jsp").forward(request, response);
        }else if (str.equals("search")){
            //获取图书名称
            String bName = request.getParameter("bName");
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //查询所有图书的数量
            int count = bookDao.bookSearchCount("%" + bName + "%");
            //定义访问的Servlet
            String targetUrl = "BookServlet";
            //定义参数
            String param = "flag=search&bName="+bName;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<Book> book = bookDao.bookSearch(new BookPage("%" + bName + "%",page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("book",book);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("pre/index.jsp?bName="+bName).forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
